package com.mz.ieann.dokit.utils

import android.content.Context
import android.os.Looper
import android.widget.Toast

/**
 * <p>------------------------------------------------------
 * <p> Copyright (C) 2021 zhangmz, All rights reserved.
 * <p> If there is technical discussion, you can contact zhangmz90@foxmail.com
 * <p>------------------------------------------------------
 * <p> des
 * <p>
 *
 * @author Created by Administrator
 * @date on 2021 2021/11/14 13:50
 */
object ToastUtils {

    private var mToast : Toast? = null

    private fun isMain() : Boolean {
        if (Looper.getMainLooper() == Looper.myLooper()){
            return false
        }
        LogUtils.e("ensure that the current thread is the main thread")
        return true
    }

    fun toast(context : Context, message : String){
        if (isMain()){
            return
        }
        mToast?.run {
            duration = Toast.LENGTH_SHORT
            setText(message)
            show()
            return
        }
        mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
        mToast?.show()
    }

    fun toastLong(context : Context, message : String){
        if (isMain()){
            return
        }
        mToast?.run {
            duration = Toast.LENGTH_LONG
            setText(message)
            show()
            return
        }
        mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
        mToast?.show()
    }

}